import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;

import java.util.Collections;

/**
 * MyBatis-Plus自动生成代码
 * @since 2022-09-07
 * @author LiuChengLong
 */
public class AutoGenerateCode {

    public static void main(String[] args) {
        String username = "LiuChengLong";
        String moduleName = "wm";
        String[] tableNameList = {"tb_wm_item", "tb_wm_item_key_word", "tb_wm_lich_ephemeras", "tb_wm_lich_quirks"
                , "tb_wm_lich_weapons", "tb_wm_riven_attributes", "tb_wm_riven_attributes_key_word", "tb_wm_riven_item"
                , "tb_wm_riven_item_key_word", "tb_wm_riven_listen", "tb_wm_sister_ephemeras", "tb_wm_sister_quirks"
                , "tb_wm_sister_weapons"};
        autoGenerateCode(username, moduleName, tableNameList);
    }

    public static void autoGenerateCode(String userName, String moduleName, String[] tableNameList) {
        String property = System.getProperty("user.dir");
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/warframebot?serverTimezone=UTC", "root", "123456")
                .globalConfig(builder -> {
                    builder.author(userName) // 设置作者
                            .outputDir(property + "/src/main/java") // 指定输出目录
                            .dateType(DateType.ONLY_DATE)
                            .disableOpenDir(); // 生成完成后不打开文件夹
                })
                .packageConfig(builder -> {
                    builder.parent("com.liu.mybatisPlus") // 设置父包名
                            .moduleName(moduleName) // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, property + "/src/main/resources/com/liu/mapper/" + moduleName)) // 设置mapperXml生成路径
                            .controller("controller")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .entity("entity.base");
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tableNameList) // 设置需要生成的表名
                            .addTablePrefix("tb_") // 设置过滤表前缀
                            .controllerBuilder()
                            .enableRestStyle() // 开启生成@RestController 控制器
                            .entityBuilder()
                            .enableLombok() // 设置entity使用lombok
                            .enableTableFieldAnnotation() // 设置entity生成字段注解
                            .mapperBuilder()
                            .enableBaseColumnList() // 启用 BaseResultMap 生成
                            .serviceBuilder()
                            .formatServiceFileName("%sService"); // 设置service类名格式
                }).execute();
    }
}
